<template>
    <div id="ArriveInformation"
        :style="{ 'transition': isCollapse ? '.6s all' : '.35s all', 'width': isCollapse ? myWidth1 : myWidth2 }">
        <div class="main" :style="{'height':myHeight}">


            <div class="s_top">
                <div class="serach">
                    <el-row>
                        <el-col :span="3" style="text-align: right;padding-right: 10px;font-size: 13px;">
                            姓名
                        </el-col>
                        <el-col :span="9">
                            <el-input v-model="serach.serachName" placeholder="请输入内容"></el-input>
                        </el-col>
                        <el-col :span="3" style="text-align: right;padding-right: 10px;font-size: 13px;">
                            学号
                        </el-col>
                        <el-col :span="9">
                            <el-input v-model="serach.serachNumber" placeholder="请输入内容"></el-input>
                        </el-col>
                    </el-row>
                </div>
                <el-button size="mini" style="margin-left:10px" @click="pageNum=1;init()">重置</el-button>
                <el-button type="primary" size="mini" @click="forSerach">搜索</el-button>
            
                <el-button type="primary" class="btn_excel" style="margin-left: 2px;" @click="forExcel">导出为Excel</el-button>
                <el-button type="success" class="btn_excel" v-show="false" style="margin-right: 0px;" @click="addArrive">添加数据</el-button>
            </div>


            <el-table :data="tableData" stripe style="width: 100%" :height="myHeight-50" id="excel">
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <el-form label-position="left" inline class="demo-table-expand">
                            <el-form-item label="缴费状态:">
                                <span>{{props.row.img==""?"未上传缴费凭证":"已上传缴费凭证"}}</span>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-table-column>
                <el-table-column type="index" width="50">
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="number" label="学号" width="180">
                </el-table-column>
                <el-table-column prop="" label="报道状态">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.userGetCheckTDO.state == '1' ? 'danger' : scope.row.userGetCheckTDO.state == 2 ? 'warning' : scope.row.userGetCheckTDO.state == 3 ? 'info' : scope.row.userGetCheckTDO.state == 4 ? 'success' : ''" disable-transitions>
                        {{ scope.row.userGetCheckTDO.state == '1' ? '未报道' : scope.row.userGetCheckTDO.state == 2 ? '已报道' : scope.row.userGetCheckTDO.state == 3 ? '已上传凭证' : scope.row.userGetCheckTDO.state==4?'已选择宿舍':'完成所有流程'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column  label="更新时间">
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span style="margin-left: 10px">{{ scope.row.userGetCheckTDO.state == "1" ? "请先报道" : scope.row.userGetCheckTDO.time }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" v-if="!notforexcel">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="updateArrive(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="deleteArrive(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        

            <div class="block" style="background-color:#ffffff">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageNum"
                    :page-size="pageSize" layout="prev, pager, next, jumper" :total="sumNum">
                </el-pagination>
            </div>
        </div>


        <!-- 编辑打开的表格 -->
        <el-drawer :title="title" :visible.sync="drawer" direction="rtl" :before-close="handleClose">
            <el-form ref="form" :model="updateData" label-width="80px" style="padding-right: 10px;">
                <el-form-item label="姓名">
                    <el-input disabled  v-model="updateData.name"></el-input>
                </el-form-item>
                <el-form-item  label="学号">
                    <el-input disabled v-model="updateData.number"></el-input>
                </el-form-item>
                
                <el-form-item label="报道状态">
                    <el-select  v-model="updateData.state" placeholder="请选择">
                        <el-option v-for="(item) in list" :key="item.name" :label="item.name" :value="item.pid">
                        </el-option>
                    </el-select>
                </el-form-item>
        
        
                <el-form-item>
                    <el-button type="primary"  @click="onUpdate">修改</el-button>
                    <el-button @click="drawer=false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data(){
        return{
            sumNum: 0,
            pageSize: 10,
            pageNum: 1,//现在的页数
            title: "",
            serach:{
                serachName:"",
                serachNumber:"",
            },
            timeData:[],
            updateData:{
                name:"",
                number:"",
                uid:"",
                state:"",
            },
            list:[],
            tableData: [],
            allData:[],
            drawer:false,
            notforexcel: false,
        }
    },
    props:{
        isCollapse: true,
        userdetail:{},
        studentdetail: {},
    },
    created() {
        this.myHeight = window.innerHeight - 40 - 40 - 20 - 30 + "";
        this.myWidth1 = window.innerWidth - 64 - 30 + "px"
        this.myWidth2 = window.innerWidth - 200 - 30 + "px"
    },
    mounted(){
        this.init();
    },
    methods:{
        handleSizeChange(val) {//页的大小改变时触发
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {//当前页切换时触发
            // console.log(`当前页: ${val}`);
            var _this = this;
            axios({
                method: "get",
                url: "api/users/getAllInfo?pageSize=" + _this.pageSize + "&pageNum=" + val,
            }).then(function (resp) {
                _this.tableData = resp.data.data.data;
            })
        },
        init(){
            var _this=this;
            axios({
                method: "get",
                url: "api/users/getAllInfo?pageSize=" + _this.pageSize + "&pageNum=" + _this.pageNum,
            }).then(function (resp) {
                _this.tableData = resp.data.data.data;
            })
            axios({
                method: "get",
                url: "api/users/getAllInfo?pageSize=1000&pageNum=1",
            }).then(function (resp) {
                // console.log(resp.data);
                _this.sumNum = resp.data.data.totalNum;
                _this.allData = resp.data.data.data;
            })
            

            axios({//获取步骤列表
                method: "get",
                url: "api/processes/getAllProcesses",
            }).then(function (resp) {
                // console.log(resp);
                _this.list = resp.data.data;
            })
            
            
        },
        forExcel() {
            var temp = this.tableData;
            this.tableData = this.allData
            this.notforexcel = !this.notforexcel;
            this.$nextTick(() => {
                this.Excels.exportExcel("导出数据.xlsx", "#excel");  //此括号里面（第一个是导出文件的名称，第二个是需要导出的标签的id）
                this.notforexcel = !this.notforexcel;
                this.tableData = temp;
            })
        },
        forSerach(){
            if (this.serach.serachName == "" && this.serach.serachNumber == "") {
                this.$message('请输入内容！');
            } else {
                var _this = this;
                // console.log(this.serach);
                if (this.serach.serachName == "") {//按学号
                    axios({
                        method: "post",
                        url: "api/users/getByNumber",
                        data: {
                            "number": _this.serach.serachNumber
                        }
                    }).then(function (resp) {
                        _this.tableData = resp.data.data.data;
                        _this.pageNum = 1;
                        _this.serach.serachNumber = "";
                        _this.sumNum = resp.data.data.totalNum;
                    })
                } else if (this.serach.serachNumber == "") {//按姓名查
                    axios({
                        method: "post",
                        url: "api/users/getByLikeName",
                        data: {
                            "name": _this.serach.serachName
                        }
                    }).then(function (resp) {
                        _this.tableData = resp.data.data.data;
                        _this.pageNum = 1;
                        _this.serach.serachName = "";
                        _this.sumNum = resp.data.data.totalNum;
                    })
                } else {//一起查
                    axios({
                        method: "post",
                        url: "api/users/getByLikeName",
                        data: {
                            "name": _this.serach.serachName
                        }
                    }).then(function (resp) {
                        // console.log(resp.data.data.data)
                        _this.tableData = [];
                        resp.data.data.data.forEach((item, index) => {
                            var index = item.number.indexOf(_this.serach.serachNumber);
                            if (index != -1) {
                                _this.tableData.push(item);
                                _this.pageNum = 1;
                                _this.sumNum = 1;
                            }
                        })
                        _this.serach.serachName = "";
                        _this.serach.serachNumber = ""
                    })
                }
            }
        },
        deleteArrive(index, row) {
            // this.tableData.splice(index, 1);
            var _this = this;
            //数据库操作
            axios({
                method: "delete",
                url: "api/users/deleteCheckById/" + row.userGetCheckTDO.cid,
            }).then(function (resp) {
                if (resp.data.code == 200) {
                    _this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    _this.pageNum=1;
                    _this.init();
                } else {
                    _this.$message({
                        message: resp.data.msg,
                        type: 'error'
                    });
                }
            })
            
        }, 
        updateArrive(index, row) {
            console.log(row);
            this.updateData.uid = row.userGetCheckTDO.userGetTDO.uid;
            this.updateData.state=row.userGetCheckTDO.state;
            this.updateData.name = row.name;
            this.updateData.number = row.number;
            this.drawer = !this.drawer;
        },
        onUpdate(){
            if (this.updateData.state=="") {
                this.$message("请输入规范内容")
            }else{
                //数据库操作
                console.log(this.updateData);
                var _this=this;
                axios({
                    method: "put",
                    url: "api/users/updateCheck",
                    data:{
                        "uid":_this.updateData.uid,
                        "state":_this.updateData.state
                    }
                }).then(function (resp) {
                    if (resp.data.code == 200) {
                        _this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        _this.pageNum = 1;
                        _this.init();
                    } else {
                        _this.$message({
                            message: resp.data.msg,
                            type: 'error'
                        });
                    }
                })

                this.drawer = !this.drawer;

            }
        },
        addArrive() {},
        onAdd() { },
        handleClose(done) {
            this.$confirm('还未保存是否关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
    }
    
}
</script>

<style scoped>
#ArriveInformation {
    float: left;
    height: 500px;
}

#ArriveInformation .main {
    width: 100%;
    margin-top: 15px;
}

#ArriveInformation .main .s_top {
    text-align: initial;
    width: 100%;
    height: 50px;
    line-height: 50px;
    background-color: #ffffff;
    border-bottom: 0.7px solid rgba(0, 0, 0, .2);
}

.serach {
    width: 650px;
    height: 100%;
    float: left;
}

.btn_excel {
    float: right;
    margin-top: 5px;
    margin-right: 10px;
}
#ArriveInformation .demo-table-expand {
    font-size: 0;
}

#ArriveInformation .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}

#ArriveInformation .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
    padding-left: 20px;
}
</style>